import { api } from "@/lib/request";
import { type Response } from "@/types/response";
import type { UpdateEmailFormData } from "@/schemas/update-email";
import type { UpdateMobileFormData } from "@/schemas/update-mobile";

export const userAPI = {
  user: async (): Promise<Response> => {
    const response = await api.get("/user");
    return response as unknown as Response;
  },
  
  updateUser: async (data: {
    image?: string;
    name?: string;
    first_name?: string;
    last_name?: string;
    address?: string;
    region?: string;
  }): Promise<Response> => {
    const response = await api.put("/user", data);
    return response as unknown as Response;
  },

  updatePassword: async (data: {
    current_password: string;
    new_password: string;
    confirm_password: string;
  }): Promise<Response> => {
    const response = await api.put("/user/password", {
      current_password: btoa(data.current_password),
      new_password: btoa(data.new_password),
      confirm_password: btoa(data.confirm_password),
    });
    return response as unknown as Response;
  },

  // 设置密码（适用于OAuth用户）
  setPassword: async (data: {
    new_password: string;
    confirm_password: string;
  }): Promise<Response> => {
    const response = await api.put("/user/password/set", {
      new_password: btoa(data.new_password),
      confirm_password: btoa(data.confirm_password),
    });
    return response as unknown as Response;
  },

  // 发送邮箱更新验证码
  sendEmailUpdateCaptcha: async (email: string): Promise<Response> => {
    const response = await api.post("/user/email/update/captcha", { email });
    return response as unknown as Response;
  },

  // 更新邮箱
  updateEmail: async (data: UpdateEmailFormData): Promise<Response> => {
    const response = await api.put("/user/email/update", data);
    return response as unknown as Response;
  },

  // 发送手机号更新验证码
  sendMobileUpdateCaptcha: async (mobile: string): Promise<Response> => {
    const response = await api.post("/user/mobile/update/captcha", { mobile });
    return response as unknown as Response;
  },

  // 更新手机号
  updateMobile: async (data: UpdateMobileFormData): Promise<Response> => {
    const response = await api.put("/user/mobile/update", data);
    return response as unknown as Response;
  },

  // 删除用户
  deleteUser: async (password?: string): Promise<Response> => {
    const data = password ? { password: btoa(password) } : {};
    const response = await api.delete("/user", { data });
    return response as unknown as Response;
  },
};
